package com.xiaobai.zhuangxiuguanjia.Utils;

import android.content.Context;
import android.util.Log;
import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.model.LatLng;

/**
 * Created by gggcccwww on 2015/6/17.
 */
public class DingWei implements BDLocationListener {
    private static DingWei dingweiinstance;
    private static Context context;
    private LocationClient locationClient;
    private LocationClientOption locationClientOption;
    private String addrStr;
    private LatLng latLng;
    //    private Marker marker;


    private DingWei(Context context){
        this.context=context;

        locationClient = new LocationClient(context.getApplicationContext());
        locationClient.registerLocationListener(this);
        locationClientOption = new LocationClientOption();
        locationClientOption.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        locationClientOption.setOpenGps(true);
        locationClientOption.setScanSpan(5000);
        locationClientOption.setCoorType("bd0911");
        locationClientOption.setIsNeedAddress(true);

        locationClient.setLocOption(locationClientOption);
        locationClient.start();
    }

    public static synchronized DingWei getInstance(Context context){
        if(dingweiinstance==null){
            dingweiinstance=new DingWei(context);
        }
        return dingweiinstance;
    }

    public String getAddrStr(){
        if(addrStr!=null){
            return addrStr;
        }else{
            addrStr="暂无定位信息";
            return addrStr;
        }
    }
    public LatLng getLatLng(){
        return latLng;
    }




    @Override
    public void onReceiveLocation(BDLocation bdLocation) {
        int locType = bdLocation.getLocType();
        switch (locType){
            case 61: // GPS 结果
            case 65: // 缓存的结果
            case 161: // 网络定位的结果
//                String province = bdLocation.getProvince();
//                String city = bdLocation.getCity();
//                Log.d("LocationTest", "当前城市:" + city);
                latLng = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude());
                addrStr = bdLocation.getAddrStr();
                Log.d("----->", addrStr);
//                if(marker==null){
//                    MarkerOptions markerOptions = new MarkerOptions();
//                    markerOptions.position(latLng);
//                    BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.ic_location_blue);
//                    markerOptions.icon(bitmapDescriptor);
////                    marker = (Marker) baiduMap.addOverlay(markerOptions);
////                    MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.newLatLngZoom(latLng,16);
////                    // 设置当前地图状态，更新为新的位置
////                    baiduMap.setMapStatus(mapStatusUpdate);
//                }
//                marker.setPosition(latLng);
                break;
            default:
                break;

        }
    }

}
